home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / VISCAFE.BIN / MenuBar.java < prev    next >
Encoding:
Java Source  |  1996-05-02  |  4.3 KB  |  170 lines

  1. /*
  2.  * @(#)MenuBar.java    1.16 96/02/29 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994,1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.util.Vector;
  22. import java.awt.peer.MenuBarPeer;
  23.  
  24. /**
  25.  * A class that encapsulates the platform's concept of a menu bar bound
  26.  * to a Frame. In order to associate the MenuBar with an actual Frame,
  27.  * the Frame.setMenuBar() method should be called.
  28.  *
  29.  * @see Frame#setMenuBar
  30.  *
  31.  * @version 1.16, 29 Feb 1996
  32.  * @author Sami Shaio
  33.  *
  34.  */
  35. public class MenuBar extends MenuComponent implements MenuContainer {
  36.     Vector menus = new Vector();
  37.     Menu helpMenu;
  38.  
  39.     /**
  40.      * Creates a new menu bar.
  41.      */
  42.     public MenuBar() {
  43.     }
  44.  
  45.     /**
  46.      * Creates the menu bar's peer.  The peer allows us to change the 
  47.      * appearance of the menu bar without changing any of the menu bar's 
  48.      * functionality.
  49.      */
  50.     public synchronized void addNotify() {
  51.     peer = Toolkit.getDefaultToolkit().createMenuBar(this);
  52.  
  53.     int nmenus = countMenus();
  54.     for (int i = 0 ; i < nmenus ; i++) {
  55.         getMenu(i).addNotify();
  56.     }
  57.     }
  58.  
  59.     /**
  60.      * Removes the menu bar's peer.  The peer allows us to change the 
  61.      * appearance of the menu bar without changing any of the menu bar's 
  62.      * functionality.
  63.      */
  64.     public void removeNotify() {
  65.     int nmenus = countMenus();
  66.     for (int i = 0 ; i < nmenus ; i++) {
  67.         getMenu(i).removeNotify();
  68.     }
  69.     super.removeNotify();
  70.     }
  71.  
  72.     /**
  73.      * Gets the help menu on the menu bar.
  74.      */
  75.     public Menu getHelpMenu() {
  76.     return helpMenu;
  77.     }
  78.  
  79.     /**
  80.      * Sets the help menu to the specified menu on the menu bar.
  81.      * @param m the menu to be set
  82.      */
  83.     public synchronized void setHelpMenu(Menu m) {
  84.     if (helpMenu == m) {
  85.         return;
  86.     }
  87.     if (helpMenu != null) {
  88.         helpMenu.removeNotify();
  89.         helpMenu.parent = null;
  90.     }
  91.     if (m.parent != this) {
  92.         add(m);
  93.     }
  94.     helpMenu = m;
  95.     if (m != null) {
  96.         m.isHelpMenu = true;
  97.         m.parent = this;
  98.         MenuBarPeer peer = (MenuBarPeer)this.peer;
  99.         if (peer != null) {
  100.         if (m.peer == null) {
  101.             m.addNotify();
  102.         }
  103.         peer.addHelpMenu(m);
  104.         }
  105.     }
  106.     }
  107.  
  108.     /**
  109.      * Adds the specified menu to the menu bar.
  110.      * @param m the menu to be added to the menu bar
  111.      */
  112.     public synchronized Menu add(Menu m) {
  113.     if (m.parent != null) {
  114.         m.parent.remove(m);
  115.     }
  116.     menus.addElement(m);
  117.     m.parent = this;
  118.  
  119.     MenuBarPeer peer = (MenuBarPeer)this.peer;
  120.     if (peer != null) {
  121.         if (m.peer == null) {
  122.         m.addNotify();
  123.         }
  124.         peer.addMenu(m);
  125.     }
  126.     return m;
  127.     }
  128.  
  129.     /**
  130.      * Removes the menu located at the specified index from the menu bar.
  131.      * @param index the position of the menu to be removed
  132.      */
  133.     public synchronized void remove(int index) {
  134.     MenuBarPeer peer = (MenuBarPeer)this.peer;
  135.     if (peer != null) {
  136.         Menu m = getMenu(index);
  137.         m.removeNotify();
  138.         m.parent = null;
  139.         peer.delMenu(index);
  140.     }
  141.     menus.removeElementAt(index);
  142.     }
  143.  
  144.     /**
  145.      * Removes the specified menu from the menu bar.
  146.      * @param m the menu to be removed
  147.      */
  148.     public synchronized void remove(MenuComponent m) {
  149.     int index = menus.indexOf(m);
  150.     if (index >= 0) {
  151.         remove(index);
  152.     }
  153.     }
  154.  
  155.     /**
  156.      * Counts the number of menus on the menu bar.
  157.      */
  158.     public int countMenus() {
  159.     return menus.size();
  160.     }
  161.  
  162.     /**
  163.      * Gets the specified menu.
  164.      * @param i the menu to be returned
  165.      */
  166.     public Menu getMenu(int i) {
  167.     return (Menu)menus.elementAt(i);
  168.     }
  169. }
  170.